#ifndef CIRCULARQUEUE_H
#define CIRCULARQUEUE_H

#include "systemHead.h"
#include "singleinstance.h"

struct Unity{
    AVPacket _apk;
    std::mutex         _mutex;
};



//两者重合时为空，两者相邻时且生产者在后面为满
class CircularQueue
{
public:
    CircularQueue();
    virtual ~CircularQueue();
    void AddElement(AVPacket *ele);
    void Consume(AVPacket *ele);
    int GetCurSize();
    void Destroy();
    //在一个队列上只有一对生产者和消费者，所以这种读不需要加锁，即便出错也只会出现不满却返回true，
    bool isFull() const
    {
//        std::lock_guard<std::mutex> lock(_mutex);
        if(_product + 1 == _consume || (_product + 1)%static_cast<int>(_arr.size()) == _consume)
        {
            return true;
        }else{
            return false;
        }
    }
    //同上理，即便出错也只会出现非空却返回true，
    bool isEmpty() const
    {
//        std::lock_guard<std::mutex> lock(_mutex);
        if(_product != _consume)
        {
            return  false;
        }else{
            return  true;
        }
    }
    int GetCacpity() const
    {
        return _arr.size();
    }
private:
    std::array<Unity, 1000> _arr;
    int                        _product = 0;
    int                        _consume = 0;
    int                        _Mid = 400;
//    mutable std::mutex         _mutex;
};

#endif // CIRCULARQUEUE_H
